package com.ruoyi.web.controller.hospital;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hospital.domain.HPatient;
import com.ruoyi.hospital.domain.HPatientPdf;
import com.ruoyi.hospital.service.IHpatientPdfService;
import com.ruoyi.hospital.service.IHpatientService;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.service.IHPatientAuditService;
import com.ruoyi.system.service.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 患者管理controller
 * @author liz
 * @date 2023-10-18 10:45
 */
@RestController
@RequestMapping("/hospital/patient")
public class HPatientController extends BaseController {

    @Autowired
    IHpatientService patientService;
    @Autowired
    IHPatientAuditService patientAuditService;
    @Autowired
    IHpatientPdfService patientPdfService;

    /**
     * 获取患者列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(HPatient patient) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        patient.setUserId(loginUser.getUserId());
        startPage();
        List<HPatient> patientList = patientService.selectPatientList(patient);
        return getDataTable(patientList);
    }

    /**
     * 获取患者列表
     */
    @GetMapping("/listAudit")
    public TableDataInfo listAudit(HPatient patient) {
        startPage();
        List<HPatient> patientList = patientService.selectPatientListAudit(patient);
        return getDataTable(patientList);
    }


    /**
     * 根据Id获取患者详情和报销详情
     *  @GetMapping(value = "/{configId}")
     *     public AjaxResult getInfo(@PathVariable
     */
    @PreAuthorize("@ss.hasPermi('hospital:patient:detail')")
    @GetMapping(value="/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        return success(patientService.selectById(id));
    }

    /**
     * 根据pdfId获取患者pdf文件
     *  @GetMapping(value = "/{configId}")
     *     public AjaxResult getInfo(@PathVariable
     */
    @PreAuthorize("@ss.hasPermi('hospital:patient:pdf')")
    @GetMapping("/pdf/{id}")
    public AjaxResult selectPdfById(@PathVariable Integer id) {
        HPatientPdf patientPdf = patientPdfService.getById(id);
        if(patientPdf != null){
            return success(patientPdf);
        } else {
            return error("该患者无费用结算单");
        }
    }

    /**
     * 根据inpatientNo获取患者pdf文件
     */
    @PreAuthorize("@ss.hasPermi('hospital:patient:pdf')")
    @GetMapping("/sqlSeverPdf/{id}")
    public AjaxResult getPdfFromSqlSever(@PathVariable String id) {
        HPatientPdf patientPdf = patientPdfService.selectPdfByInpatientNo(id);
        if(patientPdf != null){
            return success(patientPdf);
        } else {
            return error("该患者无费用结算单");
        }
    }

    /**
     * 报销申请
     */
    @PreAuthorize("@ss.hasPermi('hospital:patient:add')")
    @PostMapping
    public AjaxResult add(@RequestBody HPatient patient) {
        patientService.updateStatusById(patient.getId(),patient.getSettleType(),patient.getSettleName());
        return success();
    }
    @PreAuthorize("@ss.hasPermi('hospital:patient:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, HPatient patient)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        patient.setUserId(loginUser.getUserId());
        List<HPatient> list = patientService.selectPatientList(patient);
        ExcelUtil<HPatient> util = new ExcelUtil<HPatient>(HPatient.class);
        util.exportExcel(response, list, "岚山区人民医院患者出院报销审核");
    }
}
